﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace SharingCenter.Web.Classes.Albums
{
    using SharingCenter.Web.Classes;
    using SharingCenter.Web.Classes.enums;
    public static class AlbumOperations
    {
        public static AlbumsWithCoverImage GetAlbum(Guid albumId)
        {
            try
            {
                return scDataContext.Instance.AlbumsWithCoverImages.Single(d => d.AlbumId == albumId);
            }
            catch { return null; }
        }

        public static List<AlbumsWithCoverImage> GetAlbums(EntityType contextType, Guid contextId)
        {
            var q = from t in scDataContext.Instance.AlbumsWithCoverImages
                    where t.ContextId == contextId && t.ContextType == contextType
                    select t;
            return q.ToList();
        }

        public static List<Sc_AlbumImage> GetAlbumImages(Guid albumId)
        {
            var q = from t in scDataContext.Instance.Sc_AlbumImages
                    where t.AlbumId == albumId
                    select t;
            return q.ToList();
        }

        public static void AddImage(string fileName, Guid albumId, Guid userId, bool submitChanges)
        {
            if (!AlbumImageExists(albumId, fileName))
            {
                Sc_AlbumImage img = new Sc_AlbumImage();
                img.AlbumId = albumId;
                img.Title = fileName;
                img.ApprovalHandled = true;
                img.Approved = true;
                img.UploadDate = DateTime.Now;
                img.UserId = userId;
                img.ImageId = Guid.NewGuid();
                img.FileName = fileName;
                scDataContext.Instance.Sc_AlbumImages.InsertOnSubmit(img);
                if (submitChanges)
                {
                    scDataContext.Instance.SubmitChanges();
                }
            }
            else
            {
                scDataContext.Instance.UpdateAlbumImage(albumId, fileName);
            }
        }

        public static bool AlbumImageExists(Guid albumId, string fileName)
        {
            return Convert.ToBoolean(scDataContext.Instance.ImageFileNameExists(albumId, fileName));
        }

        public static Sc_AlbumImage GetAlbumImage(Guid imageId)
        {
            try
            {
                return scDataContext.Instance.Sc_AlbumImages.Single(d => d.ImageId == imageId);
            }
            catch { return null; }
        }
    }
}
